<?php

class TagCloudWidget extends AbstractRouteWidget implements IWebConfig
{
	protected $tagobject;

    public function loadWebConfig(SimpleXMLElement $elem)
    {
    }

    public function getTags()
    {
        $this->tagobject = $this->params['object'];
		//if (!typeOf($this->tagobject)->hasInterface('ITags')) {
		
		//}
		//print_r($this->tagobject->Tags->get());
		return $this->tagobject->Tags->getAll();
    }

	protected static function spiralSort($arrWords)
	{
        $maxDiameter = ceil(sqrt(count($arrWords)));
        $intMx = ceil($maxDiameter/2)-1;
        $intMy = ceil($maxDiameter/2)-1;
        $intMr = 1; //radius
        $intMp = 0; //position
        $intTurn = 1; //position

        foreach ($arrWords as $arrWord) {
            $arrMatrix[$intMx][$intMy] = $arrWord;
            $intMp++;

            switch ($intTurn) {
                case 1: $intMx++; break;
                case 2: $intMy++; break;
                case 3: $intMx--; break;
                case 4: $intMy--; break;
            }

            //changing direction
            if (!($intMp % $intMr)) {
                $intTurn++;

                if ($intTurn == 2 || $intTurn == 4) {
                    $intMr++;
                }
                if ($intTurn == 5) {
                    $intTurn = 1;
                }
            }
        }
        return $arrMatrix;
    }

	public function showTemplate()
	{
		$uid = intval($this->params['id']);
		
		$user = User::getById($uid);
		
		Smarty2::Singleton()->assign('profile', $user);
		Smarty2::Singleton()->display('profile/index.tpl');
	}

    public function display()
    {
        $this->view->assign('tags', self::spiralSort($this->getTags()));

        return $this->render('templates/tagcloud.tpl');
    }
}